package com.minjiang.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.system.MyDynamic;
import com.minjiang.model.system.MyMotion;
import com.minjiang.model.system.MyUser;
import com.minjiang.model.vo.MyDynamicQueryVo;
import com.minjiang.model.vo.MyDynamicVo;
import com.minjiang.model.vo.MyMotionQueryVo;
import com.minjiang.model.vo.MyMotionVo;
import com.minjiang.system.mapper.sec.MyMotionMapper;
import com.minjiang.system.service.sec.MyMotionService;
import com.minjiang.system.service.sec.MyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "运动数据管理")
@RestController
@RequestMapping("/admin/system/myMotion")
public class MyMotionController {
    @Autowired
    private MyMotionService myMotionService;
    @Autowired
    private MyUserService myUserService;

    @ApiOperation(value = "获取运动数据分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    MyMotionQueryVo dynamicQueryVo) {
        Page<MyMotion> pageParam = new Page<>(page, limit);
        IPage<MyMotion> pageModel = myMotionService.selectPage(pageParam, dynamicQueryVo);
        List<MyMotion> records = pageModel.getRecords();
        List<MyMotionVo> myMotionVos=new ArrayList<>();
        for (MyMotion record : records) {
            MyMotionVo myMotionVo = new MyMotionVo();
            BeanUtils.copyProperties(record,myMotionVo);
            MyUser myUser = myUserService.getById(record.getUserId());
            myMotionVo.setUserName(myUser.getNickname());
            myMotionVos.add(myMotionVo);
        }
        Page<MyMotionVo> myMotionVoIPage = new Page<MyMotionVo>();
        myMotionVoIPage.setTotal(pageModel.getTotal());
        myMotionVoIPage.setCurrent(pageModel.getCurrent());
        myMotionVoIPage.setRecords(myMotionVos);
        myMotionVoIPage.setSize(pageModel.getSize());
        myMotionVoIPage.setPages(pageModel.getPages());

        return Result.ok(myMotionVoIPage);
    }

    @ApiOperation(value = "根据id获取运动数据")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Integer id) {
        MyMotion dynamic = myMotionService.getById(id);
        MyUser myUser = myUserService.getById(dynamic.getUserId());
        MyMotionVo myMotionVo = new MyMotionVo();
        BeanUtils.copyProperties(dynamic,myMotionVo);
        myMotionVo.setUserName(myUser.getNickname());
        return Result.ok(myMotionVo);
    }

    @ApiOperation(value = "删除运动数据")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id){
        boolean isOk = myMotionService.removeById(id);
        if (isOk){
            return Result.ok();
        }
        return Result.fail("删除运动数据失败");
    }


    @ApiOperation(value = "更新运动数据")
    @PutMapping("/update")
    public Result updateById(@RequestBody MyMotion dynamic) {
        myMotionService.updateById(dynamic);
        return Result.ok();
    }
}
